/*******************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino, and Ramondo
Journal of Political Economy (2022)

Program: acr_replicate.do
Date: October 2022

Description: Calls programs to reproduce all tables and figures.

Notes:  These programs were implemented using STATA 16 and MATLAB2020. 
Users may need to install additional STATA packages to implement them.
	
********************************************************************************/

clear all
set more off
set memory 64g

*Set root directory here (make sure to be in the dataverse_files folder)
global root "F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files" 
*global root ".../dataverse_files" 

*Set all directories
do "${root}/set_directories.do" $root

*Log 
cap log close


*----------------------------------------------------------------------------
*Access to Orbis firm level data (YES=1, NO=0)
local orbisaccess=1
*----------------------------------------------------------------------------


****************************** FOR RESEARCHERS WITH ACCESS TO ORBIS DATA *******************************
if `orbisaccess'==1 {

display "Access to ORBIS firm level data: YES"



/*******************************************************************************
	       Firm-level data cleaning 
*******************************************************************************/
include "${cmain}/clean_01_orbis.do" /*produces a clean firm-level dta file called "${data}/firm_allyears_sales.dta" from the downloaded ORBIS file*/
include "${cmain}/orbis_mnc_agg.do" /*produces a sector-country file of MNCs operations based on orbis data*/



/*******************************************************************************
		                       Main Text -- Tables 
*******************************************************************************/

*Table 1: Estimating the composite elasticity β — to get rho 
*-----------------------------------------------
include "${cmain}/model_01_baseline.do" /*Only to be run if ORBIS firm-level data is available*/
include "${cmain}/table_01_elasticity.do"

*Table 2: Correlations with country characteristics
*-----------------------------------------------
include "${cmain}/table_02_correlation.do"

*Table 3: Sectoral firm-embedded productivity and comparative advantage
*-----------------------------------------------
include "${cmain}/table_03_phi_vs_z_rca.do"

*Table 4: Contribution of firm-embedded productivity, restricted samples 
*-----------------------------------------------
include "${cmain}/model_04_bydecile" /*Only to be run if ORBIS firm-level data is available*/
include "${cmain}/model_05_bynumofcountries" /*Only to be run if ORBIS firm-level data is available*/
include "${cmain}/table_04_restricted_samples.do"

*Table 5: Contribution of firm-embedded productivity, additional robustness
include "${cmain}/model_06_additional_robustness" /*Only to be run if ORBIS firm-level data is available*/
include "${cmain}/table_05_additional_robustness.do"



/*******************************************************************************
		                       Main Text -- Figures
*******************************************************************************/

*Figure 1: Market shares of foreign MNE affiliates, relative to France 
include "${cmain}/figure_01_market_share.do"

*Figure 2: Dev. accounting: firm-embedded productivity vs country-embedded factors
include "${cmain}/figure_02_phi_vs_z.do"

*Figure 3: Dev. accounting: firm-embedded productivity residualized by population
include "${cmain}/figure_03_phi_pop.do"

*Figure 4: Dev. accounting: Manufacturing and Services
include "${cmain}/figure_04_phi_vs_z_manufserv.do"

*Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms.
include "${cmain}/figure_05_phi_domestic_vs_foreign.do"

*Figure 6: Alternative assumptions on the technology transfer costs
include "${cmain}/model_02_alttransfercosts.do" /*Only to be run if ORBIS firm-level data is available*/
include "${cmain}/figure_06_alt_transfer_costs.do"

*Figure 7: OLS Residuals.
include "${cmain}/model_07_selection.do" /*Only to be run if ORBIS firm-level data is available*/
*run in MATLAB "${cmain}/figure_07_ols_residuals.m" file 

*Figure 8: Output gains from eliminating barriers to MNE mobility



/*******************************************************************************
		                  Appendix -- Tables 
*******************************************************************************/

*Table A1: Estimates of gravity coefficients
include "${cappendix}/table_A1_gravity_coef.do"

*Table A2: Estimates of sectoral elasticities βj
include "${cappendix}/table_A2_elasticity.do"

*Table A3: Number of observations, R-squared, and mean squared errors 
include "${cappendix}/table_A3_N_R2_MSE.do"

*Table A4: Contribution of aggregate firm-embedded productivity, by year
include "${cmain}/model_08_byyear.do"
include "${cappendix}/table_A4_phi_vs_z_byyear.do"

*Table A5: Output per-worker and firm-embedded productivity, by country
include "${cappendix}/table_A5_phi_vs_z_country.do"

*Table B.1: Number of affiliates and parents, by NAICS2
include "${cappendix}/table_B1_affiliates_parents_bysector.do"

*Table B.2: Number of foreign affiliates, by country and sector.
include "${cappendix}/table_B2_affiliates_byisocodesector.do"

*Table B.3: Number of parents, by country and sector.
include "${cappendix}/table_B3_parents_byisocodesector.do"

*Table B.4: Countries with less than 3 affiliates, by NAICS2.
include "${cappendix}/table_B4_lessthan3affiliates.do"

*Table C.1: Contribution to Var
include "${cappendix}/table_C1_contribution_variance.do"



/*******************************************************************************
		                  Appendix -- Figures 
*******************************************************************************/
*Figure A1: Data coverage: foreign-MNE revenues
include "${cappendix}/figure_A1_coverage.do" 

*Figure A2: Country effects: Alternative dependent variables
include "${cmain}/model_03_emp_va_sales.do"
include "${cappendix}/figure_A2_sales_emp_va.do" 

*Figure A3: Dev. accounting: Manufacturing sectors
include "${cappendix}/figure_A3_phi_vs_z_manufsectors.do"

*Figure A4: Dev. accounting: Service sectors
include "${cappendix}/figure_A4_phi_vs_z_servsectors.do"

*Figure A.5: Differences in firm-embedded productivity within and between sectors
include "${cappendix}/figure_A5_between_within.do"

*Figure A.6: Firm-embedded productivity and management practices.
include "${cappendix}/figure_A6_management_practices.do"

*Figure A.7: Sectoral firm-embedded productivity and outward MNE sales
include "${cappendix}/figure_A7_mnc_outward_manufserv.do"

*Figure A.8: Country effects: Alternative specification of the technology transfer costs
include "${cappendix}/figure_A8_alt_transfer_costs.do" 

*Figure F1: Firm-embedded productivity and export corrections
include "${cmain}/model_09_exclexp.do"
include "${cappendix}/figure_F1_exportcorrection.do" 

noi cls
noi dis "done!"
cap log close
exit, clear
}



****************************** FOR RESEARCHERS WITHOUT ACCESS TO ORBIS DATA *******************************
if `orbisaccess'==0 {

display "Access to ORBIS firm level data: YES"



/*******************************************************************************
		                       Main Text -- Tables 
*******************************************************************************/

*Table 1: Estimating the composite elasticity β — to get rho 
*-----------------------------------------------
include "${cmain}/table_01_elasticity.do"

*Table 2: Correlations with country characteristics
*-----------------------------------------------
include "${cmain}/table_02_correlation.do"

*Table 3: Sectoral firm-embedded productivity and comparative advantage
*-----------------------------------------------
include "${cmain}/table_03_phi_vs_z_rca.do"

*Table 4: Contribution of firm-embedded productivity, restricted samples 
*-----------------------------------------------
include "${cmain}/table_04_restricted_samples.do"

*Table 5: Contribution of firm-embedded productivity, additional robustness
*-----------------------------------------------
include "${cmain}/table_05_additional_robustness.do"



/*******************************************************************************
		                       Main Text -- Figures
*******************************************************************************/

*Figure 1: Market shares of foreign MNE affiliates, relative to France 
include "${cmain}/figure_01_market_share.do"

*Figure 2: Dev. accounting: firm-embedded productivity vs country-embedded factors
include "${cmain}/figure_02_phi_vs_z.do"

*Figure 3: Dev. accounting: firm-embedded productivity residualized by population
include "${cmain}/figure_03_phi_pop.do"

*Figure 4: Dev. accounting: Manufacturing and Services
include "${cmain}/figure_04_phi_vs_z_manufserv.do"

*Figure 5: Aggregate firm-embedded productivity: domestic vs foreign firms.
include "${cmain}/figure_05_phi_domestic_vs_foreign.do"

*Figure 6: Alternative assumptions on the technology transfer costs
include "${cmain}/figure_06_alt_transfer_costs.do"

*Figure 7: OLS Residuals.
*run in MATLAB "${cmain}/figure_07_ols_residuals.m" file 

*Figure 8: Output gains from eliminating barriers to MNE mobility



/*******************************************************************************
		                  Appendix -- Tables 
*******************************************************************************/

*Table A1: Estimates of gravity coefficients
include "${cappendix}/table_A1_gravity_coef.do"

*Table A2: Estimates of sectoral elasticities βj
include "${cappendix}/table_A2_elasticity.do"

*Table A3: Number of observations, R-squared, and mean squared errors 
include "${cappendix}/table_A3_N_R2_MSE.do"

*Table A4: Contribution of aggregate firm-embedded productivity, by year
include "${cappendix}/table_A4_phi_vs_z_byyear.do"

*Table A5: Output per-worker and firm-embedded productivity, by country
include "${cappendix}/table_A5_phi_vs_z_country.do"

*Table B.1: Number of affiliates and parents, by NAICS2
include "${cappendix}/table_B1_affiliates_parents_bysector.do"

*Table B.2: Number of foreign affiliates, by country and sector.
include "${cappendix}/table_B2_affiliates_byisocodesector.do"

*Table B.3: Number of parents, by country and sector.
include "${cappendix}/table_B3_parents_byisocodesector.do"

*Table B.4: Countries with less than 3 affiliates, by NAICS2.
include "${cappendix}/table_B4_lessthan3affiliates.do"

*Table C.1: Contribution to Var
include "${cappendix}/table_C1_contribution_variance.do"



/*******************************************************************************
		                  Appendix -- Figures 
*******************************************************************************/
*Figure A1: Data coverage: foreign-MNE revenues
include "${cappendix}/figure_A1_coverage.do" 

*Figure A2: Country effects: Alternative dependent variables
include "${cappendix}/figure_A2_sales_emp_va.do" 

*Figure A3: Dev. accounting: Manufacturing sectors
include "${cappendix}/figure_A3_phi_vs_z_manufsectors.do"

*Figure A4: Dev. accounting: Service sectors
include "${cappendix}/figure_A4_phi_vs_z_servsectors.do"

*Figure A.5: Differences in firm-embedded productivity within and between sectors
include "${cappendix}/figure_A5_between_within.do"

*Figure A.6: Firm-embedded productivity and management practices.
include "${cappendix}/figure_A6_management_practices.do"

*Figure A.7: Sectoral firm-embedded productivity and outward MNE sales
include "${cappendix}/figure_A7_mnc_outward_manufserv.do"

*Figure A.8: Country effects: Alternative specification of the technology transfer costs
include "${cappendix}/figure_A8_alt_transfer_costs.do" 

*Figure F1: Firm-embedded productivity and export corrections
include "${cappendix}/figure_F1_exportcorrection.do" 


}


noi cls
noi dis "done!"
exit, clear